package com.lx.entitys;

import lombok.Getter;
import lombok.Setter;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: chenjiang
 * @Date: 2021/11/18/21:03
 * @Description:
 */
@Setter
@Getter
public class CovidTopEntity implements WritableComparable<CovidTopEntity> {
    private String state;
    private String county;
    private long cases;

    @Override
    public String toString() {
        return state + "\t" + county + "\t" + cases;

    }

    /***
     * 排序规则 根据州state正序进行排序 如果州相同 则根据确诊数量cases倒序排序
     * @param o
     * @return
     */
    @Override
    public int compareTo(CovidTopEntity o) {
        int result;


        int i = state.compareTo(o.getState());

        if (i > 0) {
            result = 1;
        } else if (i < 0) {
            result = -1;
        } else {
            //州相等 同一个州下面按照 确诊病例数倒序排序
            result = cases > o.getCases() ? -1 : 1;
        }

        return result;
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(state);
        out.writeUTF(county);
        out.writeLong(cases);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.state = in.readUTF();
        this.county = in.readUTF();
        this.cases = in.readLong();
    }
}
